package com.jmb.api.web.controller.role;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.api.common.result.Result;
import com.jmb.api.entity.PermissionRoleRef;
import com.jmb.api.entity.Role;
import com.jmb.api.vo.permission.PermissionRoleRefVO;
import com.jmb.api.vo.role.RoleQueryVO;
import com.jmb.api.vo.role.RoleVO;
import com.jmb.api.web.mapper.PermissionRoleRefMapper;
import com.jmb.api.web.service.PermissionRoleRefService;
import com.jmb.api.web.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/web/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionRoleRefService permissionRoleRefService;

    // 新增更新校色
    @Operation(summary = "新增更新角色")
    @PostMapping("/saveOrUpdateRole")
    public Result saveOrUpdateRole(@RequestBody @Valid Role role){
        roleService.saveOrUpdateRole(role);
        return Result.ok();
    }

    // 角色信息
    @Operation(summary = "角色信息")
    @GetMapping("/getRole")
    public Result<Role> getRole(@RequestParam Long id){
        Role role = roleService.getById(id);
        return Result.ok(role);

    }
    // 角色列表
    @Operation(summary = "角色列表")
    @PostMapping("/listRole")
    public Result<IPage<Role>> listRole( RoleQueryVO queryVO){
        IPage<Role> roleVOPage = new Page<>(queryVO.getPageNum(), queryVO.getPageSize());
        IPage<Role> roleVOList = roleService.pageRoleByQuery(roleVOPage, queryVO);
        return Result.ok(roleVOList);
    }

    // 删除角色
    @Operation(summary = "删除角色")
    @DeleteMapping("/deleteRole")
    public Result deleteRole(@RequestParam Long id){
        roleService.removeById(id);
        return Result.ok();
    }

    // 新增权限
    @Operation(summary = "新增角色权限")
    @PostMapping("/addPermission")
    public Result addPermission(@RequestBody PermissionRoleRef permissionRoleRef){
        permissionRoleRefService.saveOrUpdate(permissionRoleRef);
        return Result.ok();
    }
    // 删除权限
    @Operation(summary = "删除角色权限")
    @DeleteMapping("/deletePermission")
    public Result deletePermission(@RequestBody PermissionRoleRef permissionRoleRef){
        permissionRoleRefService.removePermission(permissionRoleRef);
        return Result.ok();
    }
    // 查询角色权限
    @Operation(summary = "查询角色权限")
    @GetMapping("/permission/{id}")
    public Result<List<PermissionRoleRefVO>> permission(@PathVariable Long id){
        List<PermissionRoleRefVO> permissionIds = permissionRoleRefService.selectPermissions(id);
        return Result.ok(permissionIds);
    }


}
